package com.fzf.org.test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fzf.org.enums.AuditEnums;
import com.fzf.org.mq.producer.BlogReviewerProducer;
import com.fzf.org.service.BlogAuditService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Redis Stream消息队列测试控制器
 * 用于测试博客审核消息的发送和接收过程
 */
@Slf4j
@RestController
@RequestMapping("/api/test/redis-stream")
@RequiredArgsConstructor
public class RedisStreamTestController {

    private final BlogAuditService blogAuditService;
    private final BlogReviewerProducer blogReviewerProducer;
    private final ObjectMapper objectMapper;
    
    // 模拟接收来自消费者的HTTP请求的端点
    @PostMapping("/mock-review")
    public ResponseEntity<Map<String, Object>> mockReviewEndpoint(@RequestBody ReviewRequest request) {
        log.info("Received mock review request: {}", request);
        
        // 构造响应
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", "Mock review processed successfully");
        response.put("data", request);
        
        return ResponseEntity.ok(response);
    }
    
    // 通过服务层发送消息
    @PostMapping("/send-via-service")
    public ResponseEntity<Map<String, Object>> sendViaService(
            @RequestParam Long blogId) {
        
        log.info("Sending blog audit message via service - blogId: {}, action: {}, reason: {}", 
                blogId);

        
        boolean success = blogAuditService.sendBlogAuditRequest(blogId);
        
        if (success) {
            return ResponseEntity.ok(createResponse(true, "Message sent successfully via service"));
        } else {
            return ResponseEntity.internalServerError()
                    .body(createResponse(false, "Failed to send message via service"));
        }
    }
    
    // 直接通过生产者发送消息
    @PostMapping("/send-direct")
    public ResponseEntity<Map<String, Object>> sendDirect(
            @RequestParam Long blogId) {
        
        log.info("Sending blog audit message directly - blogId: {}, action: {}, reason: {}", 
                blogId);
        
        try {
            var recordId = blogReviewerProducer.sendBlogReviewMessage(blogId);
            
            Map<String, Object> response = createResponse(true, "Message sent successfully");
            response.put("messageId", recordId.getValue());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("Error sending message directly", e);
            return ResponseEntity.internalServerError()
                    .body(createResponse(false, "Error: " + e.getMessage()));
        }
    }
    
    private Map<String, Object> createResponse(boolean success, String message) {
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", message);
        return response;
    }
    
    @Data
    public static class ReviewRequest {
        private Long blogId;
    }
}